package com.dky.satell.mybatis_plus_genrator;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * 代码生成器
 *
 * @author Mark
 */
public class CodeGenerator {


    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("mqc");
        //  gc.setMapperName("%sDao");//不设置以Mapper结尾
        //gc.setXmlName("%sDao");//不设置以Mapper结尾
        gc.setServiceName("%sService");//不设置默认为I%Service
        gc.setServiceImplName("%sServiceImpl");//不设置默认Impl结尾
        gc.setControllerName("%sRestController");//不设置默认为%sController
        gc.setOpen(false);
        gc.setDateType(DateType.TIME_PACK);
        gc.setBaseColumnList(true);
        gc.setBaseResultMap(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/async_proxy?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setTypeConvert(new MySqlTypeConvert());
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("satell");
        pc.setParent("com.dky");
        pc.setMapper("mapper");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setController("controller");
        pc.setXml("mapper.xml");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setEntityLombokModel(true); // 自动lombok
//        strategy.setSuperControllerClass("com.botianzu.growingsys.common.BaseRestController");
        //正则匹配
        /*strategy.setInclude("sys[\\w]*"); // 需要生成的表*/
        strategy.setInclude("student"); // 需要生成的表
        strategy.setLogicDeleteFieldName("deleted");//设置逻辑字段
//        strategy.setLogicDeleteFieldName("is_deleted");
        /* strategy.setSuperEntityClass("com.botianzu.growingsys.common.BaseEntity"); */
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(false);

        /* strategy.setEntityLombokModel(true); */
        // 填写BaseEntity中的公共字段
        strategy.setSuperEntityColumns(new String[]{
                "deleted", "create_time", "update_time"
        });

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}